/**
 * 
 */
(function() {
	/** ************************START********************************** */
	//	$(document).keydown(function(event) {
	//		platform.forbiddenEnter(event);
	//	});

	window.onload = function() {
		permGridView.init();
	};

	/***************************************************************************
	 * permGridView
	 */
	var permGridView = {
		/**
		 * method
		 */
		/**
		 * 初始化
		 */
		init: function() {
			var columns = [{
				checkbox: true,
				field: 'checkbox'
			}, {
				field: 'perm_id',
				title: '主键标识',
				width: 120,
				valign: 'middle',
				align: 'center',
				visible: false
			}, {
				field: 'perm_code',
				title: '权限编码',
				width: 80,
				valign: 'middle'
			}, {
				field: 'perm_name',
				title: '权限名称',
				width: 200,
				valign: 'left'
			}];
			$('#tb_perms').bootstrapTable({
				url: platform.mapUrl('~/rest/perm/list'),
				cache: false,
				method: 'post',
				contentType: 'application/json',
				dataType: 'json',
				search: false,
				strictSearch: true,
				singleSelect: true,
				striped: true,
				showFooter: false,
				showToggle: false,
				showRefresh: true,
				showPaginationSwitch: false,
				silent: true,
				pagination: true,
				sidePagination: 'server',
				pageNumber: 1,
				pageSize: 10,
				pageList: [10, 15, 20, 50],
				queryParams: function(params) {
					return {
						pageNumber: params.offset / params.limit + 1,
						pageSize: params.limit
					}
				},
				clickToSelect: true,
				height: 504,
				uniqueId: 'perm_id',
				permView: false,
				cardView: false,
				toolbar: '#toolbar',
				columns: columns
			});
		},
		fnSearch: function() {
			$("#tb_perms").bootstrapTable("refresh", {
				query: {
					perm_name: $("#name").val() == "" ? null : $("#name").val()
				}
			});
		},
		fnAdd: function() {
			headerModel.pv_title('权限信息录入');
			permView.clear();
			permView.initForm();
			permView.validateForm();
			$('#permModal').modal('show');
		},
		fnRemove: function() {
			var result = platform.checkSelected('tb_perms', 'remove');
			if(result) {
				platform.confirm('系统提示', 'small', '确认删除权限?', '确认', '取消',
					function() {
						var selections = $('#tb_perms').bootstrapTable(
							'getSelections');
						var perm_id = selections[0].perm_id;
						methods.del(perm_id);
					});
			}

		},
		fnEdit: function() {},
		fnView: function() {}
    };
	ko.applyBindings(permGridView, document.getElementById('permList'));

	var headerModel = {
		pv_title: ko.observable()
	};
	ko.applyBindings(headerModel, document.getElementById('modalHeader'));

	/***************************************************************************
	 * permView
	 */
	var permView = {
		/**
		 * parameter
		 */
		pv_permid: ko.observable(),
		pv_permname: ko.observable(),
		pv_permcode: ko.observable(),
		pv_appid: ko.observable(),
		pv_editstatus: ko.observable(), // 默认数据状态
		pv_recordstatus: ko.observable(), // 默认数据状态
		pv_enabled: ko.observable(false), // 只读设置

		/**
		 * method
		 */
		initForm: function() {
			this.pv_permid(platform.uuid());
			this.pv_editstatus('0');
			this.pv_recordstatus('1');
			this.pv_enabled(true);
		},
		clear: function() {
			this.pv_permid('');
			this.pv_permname('');
			this.pv_permcode('');
			this.pv_appid('');
			this.pv_editstatus('');
			this.pv_recordstatus('');
			this.pv_enabled(false);
		},
		validateForm: function() {
			$('#detailForm').bootstrapValidator({
				message: 'This value is not valid',
				feedbackIcons: {
					valid: 'glyphicon glyphicon-ok',
					invalid: 'glyphicon glyphicon-remove',
					validating: 'glyphicon glyphicon-refresh'
				},
				excluded: [':disabled'],
				fields: {
					permCode: {
						validators: {
							notEmpty: {
								message: '权限编码为必填字段,不可为空'
							}
						}
					},
					permName: {
						validators: {
							notEmpty: {
								message: '权限名称为必填字段,不可为空'
							}
						}
					}
				}
			});
		},
		resetForm: function() {
			if($('#detailForm').data('bootstrapValidator') != null ||
				$('#detailForm').data('bootstrapValidator') != undefined) {
				$('#detailForm').data('bootstrapValidator').resetForm();
			}
		}
    };
	ko.applyBindings(permView, document.getElementById('detailForm'));

	/***************************************************************************
	 * permFunctionView
	 */
	var permFunctionView = {
		fnSaveForm: function() {
			$('#detailForm').data('bootstrapValidator').validate();
			if(!$('#detailForm').data('bootstrapValidator').isValid()) {
				platform.showOperTip('VALIDATEFAIL');
				return;
			}
			var form = permView;
			var param = {};
			param.perm_id = form.pv_permid();
			param.perm_code = form.pv_permcode();
			param.perm_name = form.pv_permname();
			param.edit_status = form.pv_editstatus();
			param.record_status = form.pv_recordstatus();
			var result = methods.repeatCheck(param.perm_code);
			if(result.statusCode) {
				methods.save(param);
			} else {
				platform.showErrorTip(result.msg);

			}
		},
		fnCloseDlg: function() {
			permView.resetForm();
			$('input', 'form').attr('readonly');
			$('#permModal').modal('hide');
		}
    };
	ko.applyBindings(permFunctionView, document.getElementById('modalFooter'));

	/***************************************************************************
	 * methods
	 */
	var methods = {
		repeatCheck: function(permCode) {
			var param = {};
			param.perm_code = permCode;
			var result = JSON2.parse(platform.servicebreak(
				'~/rest/perm/repeatCheck', param).responseText);
			return result;
		},
		del: function(pid) {
			var param = {};
			param.perm_id = pid;
			platform.servicebreakAsync('~/rest/perm/delete', param, 'post')
				.done(function(response) {
					if(response.success) {
						platform.showOperTip('DELSUC');
						$('#tb_perms').bootstrapTable('refresh');

                    } else {
						plaform.showOperTip('DELSUC');

                    }
				});
		},
		save: function(param) {
			platform.servicebreakAsync('~/rest/perm/save', param, 'post',
				function() {
					platform.showOperTip('dealing');
				}).done(function() {
				platform.showOperTip('savesuc');
				$('#tb_perms').bootstrapTable('refresh');
			});
		}
	}
})(this);